package org.jvnet.hudson.client_demo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Dom4jTest {

	public static void main(String[] args) {
		try {
			init();
			add();
			
			edit(1);
			del(2);
			traverse();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void del(int i) throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("address.xml"));
		Element root = document.getRootElement();

		@SuppressWarnings("unchecked")
		Iterator<Element> it = root.elementIterator();

		while (it.hasNext()) {
			Element e = it.next();
			if (Integer.parseInt(e.attributeValue("id")) == i) {
				root.remove(e);
				break;
			}

		}

		OutputFormat out = new OutputFormat("  ", true, "utf-8");
		XMLWriter writer = new XMLWriter(new FileOutputStream("address.xml"),
				out);
		writer.write(document);
		writer.close();
	}

	private static void edit(int i) throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("address.xml"));
		Element root = document.getRootElement();

		Iterator<Element> it = root.elementIterator();

		while (it.hasNext()) {
			Element e = it.next();
			System.out.println( e.getDocument().getName());
			System.out.println( e.getDocument().getNodeTypeName());
			System.out.println( e.getDocument().getXMLEncoding());
			System.out.println( e.getDocument().getUniquePath());
			if (Integer.parseInt(e.attributeValue("id")) == i) {
				e.element("name").setText("你好");
				e.element("email").setText("lichao51126@gmail.com");
				break;
			}

		}

		OutputFormat out = new OutputFormat("  ", true, "utf-8");
		XMLWriter writer = new XMLWriter(new FileOutputStream("address.xml"),
				out);
		writer.write(document);
		writer.close();
	}

	private static void traverse() throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("address.xml"));
		Element root = document.getRootElement();

		@SuppressWarnings("unchecked")
		Iterator<Element> it = root.elementIterator();

		while (it.hasNext()) {
			Element e = it.next();
			System.out.println("id ：" + e.attributeValue("id"));
			System.out.println("姓名： " + e.element("name").getText());
			System.out.println("郵箱：" + e.elementText("email"));
			System.out.println("性別：" + e.elementText("gender"));
		}

	}

	private static void add() throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("address.xml"));
		Element root = document.getRootElement();

		Element parent = root.addElement("linkman");
		

		parent.addAttribute("id", "2");
		

		Element name = parent.addElement("name");
		name.setText("李麗");

		Element email = parent.addElement("email");
		email.setText("lichao511216@163.com");

		Element gender = parent.addElement("gender");
		gender.setText("female");

		OutputFormat out = new OutputFormat("  ", true, "utf-8");
		XMLWriter writer = new XMLWriter(new FileOutputStream("address.xml"),
				out);
		writer.write(document);
		writer.close();

	}

	private static void init() throws IOException {
		Document document = DocumentHelper.createDocument();

		Element root = document.addElement("addresslist");

		Element parent = root.addElement("linkman");

		parent.addAttribute("id", "1");

		Element name = parent.addElement("name");
		name.setText("李超");

		Element email = parent.addElement("email");
		email.setText("953166286@qq.com");

		Element gender = parent.addElement("gender");
		gender.setText("male");

		OutputFormat out = new OutputFormat("  ", true, "utf-8");
		XMLWriter writer = new XMLWriter(new FileOutputStream("address.xml"), out);
		writer.write(document);
		writer.close();

	}
}
